 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.restapi.Rate;
 import com.oanda.blackberry.ui.OandaMainScreen;
 import com.oanda.blackberry.util.AbstractController;
 import com.oanda.blackberry.util.AbstractViewPanel;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.PropertyChangeEvent;
 import com.oanda.blackberry.util.Settings;
 import net.rim.device.api.system.Application;
 import net.rim.device.api.ui.Field;
 import net.rim.device.api.ui.Manager;
 import net.rim.device.api.ui.MenuItem;
 import net.rim.device.api.ui.Screen;
 import net.rim.device.api.ui.UiApplication;
 import net.rim.device.api.ui.component.Menu;
 import net.rim.device.api.ui.container.MainScreen;
 import net.rim.device.api.ui.container.VerticalFieldManager;
 import net.rim.device.api.ui.decor.BackgroundFactory;
 
 public class RatesSymbolsView extends OandaMainScreen
   implements AbstractViewPanel
 {
   DefaultController controller;
   SymbolsListField list;
   VerticalFieldManager listManager = new VerticalFieldManager();
 
   private boolean warningShown = false;
 
   MenuItem backMenuItem = new MenuItem("Back", 10, 10000)
   {
     public void run() {
       RatesSymbolsView.this.popScreen();
     }
   };
 
   public RatesSymbolsView(DefaultController controller)
   {
     this.controller = controller;
     updateTitle("Rates");
     getMainManager().setBackground(BackgroundFactory.createSolidBackground(4473924));
     add(this.listManager);
   }
 
   private void setRates(final Rate rate) {
     if ((rate != null) && (rate.getSymbols() != null))
       UiApplication.getUiApplication().invokeLater(new Runnable() {
         private final Rate _rate=rate;
 
         public void run() { if (RatesSymbolsView.this.list == null) {
             RatesSymbolsView.this.list = new SymbolsListField(this._rate.getSymbols(), RatesSymbolsView.this.controller);
             RatesSymbolsView.this.listManager.add(RatesSymbolsView.this.list.getList());
           }
         }
       });
   }
 
   public void modelPropertyChange(PropertyChangeEvent evt)
   {
     if (evt.getPropertyName().equals("setSettings")) {
       if ((((Settings)evt.getNewValue()).getRate() != null) && 
         (((Settings)evt.getNewValue()).getRate() != null)) {
         setRates(((Settings)evt.getNewValue()).getRate());
       }
 
     }
     else if (evt.getPropertyName().equals("showListSymbols")) {
       this.controller.pushScreen(this);
     }
     else if (evt.getPropertyName().equals("setRateSymbol"))
     {
       setRates((Rate)evt.getNewValue());
     }
     else if (evt.getPropertyName().equals("notUseSymbol")) {
       diplayError((String)evt.getNewValue() + " is now inactive.", 1500);
     }
     else if (evt.getPropertyName().equals("setActiveSymbol"))
       diplayError((String)evt.getNewValue() + " is now active.", 1500);
   }
 
   protected void onUiEngineAttached(boolean attached)
   {
     if (attached);
   }
 
   public boolean onClose()
   {
     String s = Model.getModel().getRateSymbolsSelected();
     if ((s != null) && (s.length() > 0))
     {
       Model.getModel().getRate(); Rate.setSince(0L);
       Model.getModel().getRate().clearQuotes();
 
       this.controller.propertyChange(new PropertyChangeEvent(this, "onCloseSymbolsView", null, this.controller));
       this.controller.popScreen(this);
     } else {
       if (!this.warningShown) {
         DefaultController.inform("Please select a currency pair");
         this.warningShown = true;
         return false;
       }
       this.controller.popScreen(this);
     }
 
     return true;
   }
 
   protected void makeMenu(Menu menu, int instance)
   {
     menu.add(this.backMenuItem);
   }
 
   private void popScreen()
   {
     onClose();
   }
}

